package com.gjintel.framework.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gjintel.framework.dict.model.Dictionary;
//import com.gjintel.framework.goodstype.model.GoodsType;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.register.model.RegisterUser;
import com.gjintel.framework.role.model.Role;
import com.gjintel.framework.user.dao.IUserDao;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.user.model.UserRole;
import com.gjintel.framework.user.service.IUserService;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.base.service.impl.BaseServiceImpl;
import com.gjintel.sdk.util.StringUtil;

/**
 * @description 用户业务实现
 * @author fengya
 * @date 2015-7-31 下午01:17:33
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements IUserService {
	@Autowired
	private IUserDao userDao;

	@SuppressWarnings("unchecked")
	public List<User> findAll(Page page, User user, Sort sort,
			int loginUserType, String loginUserDeptUnit,String roles) {
		return this.userDao.findAll(page, user, sort, loginUserType,
				loginUserDeptUnit,roles);
	}

	@SuppressWarnings("unchecked")
	public void roleUsers(Page page, String roleId, Sort sort, boolean roleUser) {
		this.userDao.roleUsers(page, roleId, sort, roleUser);
	}

	@Override
	public void removeData(String deleteIds) {
		User user = null;
		RegisterUser rUser = null;
		for (String id : deleteIds.split(",")) {
			user = this.get(User.class, id);			
			if (user != null) {
				user.setFlag(1);
				rUser  = this.get(RegisterUser.class, user.getRegisterId());
				 if(rUser !=null){
					 rUser.setRemoveFlag(1);
				 }
			}
		}
	}

	public List<Role> getUserRole(String userId) {
		return this.userDao.getUserRoles(userId);
	}

	@Transactional
	public List<UserRole> bindRole(User sUser, String userIdStr, String roleId) {
		String[] userIds = userIdStr.split(",");
		UserRole ur = null;
		List<UserRole> urList = new ArrayList<UserRole>(userIds.length);
		// this.remove(this.userDao.getUserRole(roleId));
		for (String userId : userIds) {
			ur = new UserRole();
			ur.setRoleId(roleId);
			ur.setUserId(userId);
			ur.setCreaterId(sUser.getId());
			ur.setCreaterName(sUser.getName());
			ur.setCreateTime(new Date());
			urList.add(ur);
		}
		return urList;
	}

	@Override
	public List<User> login(String loginName) {
		return this.userDao.login(loginName);
	}
	
	@Override
	public List<User> getUserByCompany(String company) {
		return this.userDao.login(company);
	}

	@Override
	public List<User> findAll() {
		return this.userDao.findAll();
	}

	@Override
	public void getUsers(Page page, String userType, Sort sort) {
		this.userDao.getUsers(page, userType, sort);
	}

	@Override
	@Transactional
	public void saveRightData(User sUser, String userId, String roleIds,
			String rightVal, String goodsTypeIds) {
		if (StringUtil.isEmpty(userId))
			return;
		if (StringUtil.isEmpty(roleIds) && StringUtil.isEmpty(rightVal)
				&& StringUtil.isEmpty(goodsTypeIds))
			return;
		if (!StringUtil.isEmpty(roleIds)) {
			this.userDao.removeUserRole(userId);
			LogUtil.addLog(new LogInfo("删除用户角色", sUser.getId(),
					sUser.getName(), String.valueOf(LogType.DELETE.ordinal()),
					UserRole.class.getSimpleName(), userId));
			this.userDao.saveUserRole(sUser, userId, roleIds);
			LogUtil.addLog(new LogInfo("添加用户角色", sUser.getId(),
					sUser.getName(), String.valueOf(LogType.ADD.ordinal()),
					UserRole.class.getSimpleName(), userId));

		}
		// if (!StringUtil.isEmpty(rightVal)) {
		// this.userDao.removeUserRight(userId);
		// LogUtil.addLog(new LogInfo("删除用户权限", sUser.getId(), sUser.getName(),
		// String.valueOf(LogType.DELETE.ordinal()),
		// UserRight.class.getSimpleName(), userId));
		// this.userDao.saveUserRight(userId, rightVal);
		// LogUtil.addLog(new LogInfo("添加用户权限", sUser.getId(), sUser.getName(),
		// String.valueOf(LogType.ADD.ordinal()),
		// UserRight.class.getSimpleName(), userId));
		// }
		// if (!StringUtil.isEmpty(goodsTypeIds)) {
		// this.userDao.removeGoodsType(userId);
		// LogUtil.addLog(new LogInfo("删除商品审核设置", sUser.getId(),
		// sUser.getName(), String.valueOf(LogType.DELETE.ordinal()),
		// UserGoodsType.class.getSimpleName(), userId));
		// this.userDao.saveUserGoodsType(userId, goodsTypeIds);
		// LogUtil.addLog(new LogInfo("添加商品审核设置", sUser.getId(),
		// sUser.getName(), String.valueOf(LogType.ADD.ordinal()),
		// UserGoodsType.class.getSimpleName(), userId));
		// }

	}

	@Override
	public String getUserRoleData(String userId) {
		List<Role> userRoles = this.userDao.getUserRoles(userId);
		StringBuilder userRolesStr = new StringBuilder();
		for (int i = 0; i < userRoles.size(); i++) {
			if (i == userRoles.size() - 1) {
				userRolesStr.append(userRoles.get(i).getId());
			} else {
				userRolesStr.append(userRoles.get(i).getId()).append(",");
			}
		}
		return userRolesStr.toString();
	}

	@Override
	public String getUserRightData(String userId) {
		List<Dictionary> userRights = this.userDao.getUserRights(userId);
		StringBuilder userRightStr = new StringBuilder();
		for (int i = 0; i < userRights.size(); i++) {
			if (i == userRights.size() - 1) {
				userRightStr.append(userRights.get(i).getValue());
			} else {
				userRightStr.append(userRights.get(i).getValue()).append(",");
			}
		}
		return userRightStr.toString();
	}

	@Override
	public List<Dictionary> getUserRights(String userId) {
		return this.userDao.getUserRights(userId);
	}

	@Override
	public List<User> caLogin(String caName) {
		return this.userDao.caLogin(caName);
	}

	@Override
	public List<User> getLoginUser(String loginName) {
		return this.userDao.getLoginUser(loginName);
	}

	@Override
	public List<User> getLoginUserByLoginName(String loginName) {
		return this.userDao.getLoginUserByLoginName(loginName);
	}

	@Override
	public User getUserByRegisterId(String registerId) {
		return this.userDao.getUserByRegisterId(registerId);
	}


}
